﻿using System;
using System.Collections.Generic;
using System.Linq;
using Nxg.Blog.Core.Documents;
namespace Nxg.Blog.Core.ViewProjections.Home {
    public class IntervalBlogPostsViewModel {
        public IEnumerable<BlogPost> Posts { get; set; }

        public DateTime FromDate { get; set; }

        public DateTime ToDate { get; set; }
    }

    public class IntervalBlogPostsBindingModel {
        public DateTime FromDate { get; set; }

        public DateTime ToDate { get; set; }
    }

    public class IntervalBlogPostsViewProjection : IViewProjection<IntervalBlogPostsBindingModel, IntervalBlogPostsViewModel> {
        private readonly DataContext _db;

        public IntervalBlogPostsViewProjection (DataContext db) {
            _db = db;
        }

        public IntervalBlogPostsViewModel Project (IntervalBlogPostsBindingModel input) {
            var posts = _db.Post.Where (x => x.Status == PublishStatus.Published &&
                    x.PubDate <= DateTime.Now &&
                    x.PubDate < input.ToDate &&
                    x.PubDate > input.FromDate)
                .OrderByDescending (x => x.PubDate)
                .ToList ();
            return new IntervalBlogPostsViewModel {
                Posts = posts,
                    FromDate = input.FromDate,
                    ToDate = input.ToDate
            };
        }
    }
}